package com.example.zhu95.mytimber.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.entity.SongData;
import com.example.zhu95.mytimber.nowplaying.BaseTimber;

import butterknife.ButterKnife;

/**
 * @author zhu95
 */
public class NowPlayingActivity extends BaseActivity {
    static int currentTimberNum = 2;
    public static final String TIMBER = "com.example.zhu95.mytimber.nowplaying.Timber";
    BaseTimber currentTimber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_now_playling);
        ButterKnife.bind(this);

        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle("");
        }

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        try {
            Class<? extends BaseTimber> aTimber = (Class<? extends BaseTimber>) Class.forName(TIMBER + currentTimberNum);
            currentTimber = aTimber.newInstance();
            transaction.replace(R.id.now_playing_container, currentTimber);
            transaction.commit();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_option_popup, menu);
        menu.findItem(R.id.action_order_by).setVisible(false);
        menu.findItem(R.id.action_viewas).setVisible(false);
        menu.findItem(R.id.action_search).setIcon(R.drawable.ic_search_white_24dp);
        menu.add(getString(R.string.go_artist)).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(NowPlayingActivity.this, MainActivity.class);
                intent.putExtra(getString(R.string.go_artist), SongData.findSongByID(songid).getArtistId());
                startActivity(intent);
                return false;
            }
        });
        menu.add(getString(R.string.go_album)).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(NowPlayingActivity.this, MainActivity.class);
                intent.putExtra(getString(R.string.go_album), SongData.findSongByID(songid).getAlbumId());
                startActivity(intent);
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
            default:
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void updateUI(int songid, String musicState, int currentPosition) {
        super.updateUI(songid, musicState, currentPosition);
        currentTimber.updateUI(songid, musicState, currentPosition);
    }

    @Override
    public void updateProgress(int songid, int currentPosition) {
        super.updateProgress(songid, currentPosition);
        currentTimber.updateProgress(songid, currentPosition);
    }
}
